package com.btpn.custody.dao.hibernate;

import java.io.Serializable;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.btpn.custody.dao.ICIF;
import com.btpn.custody.entity.CIF;

@Repository("cifDaoHibernate")
public class CIF_DAO implements ICIF {

	protected HibernateTemplate template = null;
	
	@Autowired @Required
	public void setSessionFactory(SessionFactory sessionFactory) {
		template = new HibernateTemplate(sessionFactory);
	}

	@Override
	public CIF get(String sid) {
		return template.get(CIF.class, sid);
	}
	
	@Override
	public List<CIF> findAll() {
		return template.loadAll(CIF.class);
	}
	
	@Override
	public Integer getCount() {
		return Integer.valueOf(findAll().size());
	}
	
	@Override
	public Serializable create(CIF cif) {
		return template.save(cif);
	}

	@Override
	public CIF update(CIF cif) {
		template.update(cif);
		
		return cif;
	}

	@Override
	public void createOrUpdate(CIF cif) {
		template.saveOrUpdate(cif);
	}
	
	@Override
	public void delete(String sid) {
		template.delete(get(sid));
	}

	@SuppressWarnings("unchecked")
	@Override
	public CIF getByCIF(String cif) {
		List<CIF> result = template.findByNamedQueryAndNamedParam(CIF.NQ__GET_BY_CIF, "cif", cif);
		
		if (result.size()>0) {
			return result.get(0);
		} else {
			return null;
		}
	}
}